package com.zhiteng.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiteng.entity.AdjustBill;
import com.zhiteng.entity.Bill;
import com.zhiteng.entity.Procins;
import com.zhiteng.mapper.AdjustBillMapper;
import com.zhiteng.mapper.ProcinsMapper;
import com.zhiteng.service.AdjustBillService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class AdjustBillServiceImpl implements AdjustBillService {

	@Autowired
	private AdjustBillMapper adjustBillMapper;

	@Autowired
	private ProcinsMapper ProcinsMapper;

	@Override
	public int saveAdjustBill(AdjustBill adjustBill) {
		return adjustBillMapper.insertSelective(adjustBill);
	}

	@Override
	public List<AdjustBill> queryAdjustBillByTeacherId(String teacherId) {
		AdjustBill record = new AdjustBill();
		record.setTeacher_id(teacherId);
		return adjustBillMapper.select(record);
	}

	@Override
	public List<Procins> queryProcinsByAdjustBillId(Long billId) {
		Procins record = new Procins();
		record.setBill_id(billId);
		return ProcinsMapper.select(record);
	}

	@Override
	public List<AdjustBill> queryAdjustBillByPetitionerId(String petitionerId) {
		AdjustBill record = new AdjustBill();
		record.setPetitioner_id(petitionerId);
		return adjustBillMapper.select(record);
	}

	@Override
	public AdjustBill queryAdjustBillByBillId(Long bill_id) {
		AdjustBill record = new AdjustBill();
		record.setId(bill_id);
		return adjustBillMapper.selectOne(record);
	}

	@Override
	public List<AdjustBill> queryAllAdjustBillBySchoolId(String schoolId) {
		AdjustBill record = new AdjustBill();
		record.setSchool_id(schoolId);
		return adjustBillMapper.select(record);
	}

	@Override
	public List<AdjustBill> queryAllAdjustBillBySchoolIdAndPetitionerId(String schoolId) {

//		AdjustBill record=new AdjustBill();
//		record.setSchool_id(schoolId);
//		record.set
//		adjustBillMapper.select(record);

		Example example = new Example(AdjustBill.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("school_id", schoolId);
		criteria.andIsNull("petitioner_id");
		return adjustBillMapper.selectByExample(example);
	}

	@Override
	public int deleteAdjustBill(Long billId) {
		AdjustBill record = new AdjustBill();
		record.setId(billId);
		return adjustBillMapper.delete(record);
	}

	@Override
	public int updateStatus(Long billId, int i) {
		AdjustBill record = new AdjustBill();
		record.setId(billId);
		record.setStatus(i);
		record.setRevocable(1);
		return adjustBillMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<AdjustBill> queryAdjustBillBySchoolIdAndStatus(String schoolId, int i) {
		AdjustBill record = new AdjustBill();
		record.setSchool_id(schoolId);
		record.setStatus(i);
		return adjustBillMapper.select(record);
	}

	@Override
	public void updateRevocable(Long billId, int i) {
		AdjustBill record = new AdjustBill();
		record.setId(billId);
		record.setRevocable(i);
		adjustBillMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<AdjustBill> queryAllAdjustBillBySchoolId(String schoolId, Integer type2) {
		AdjustBill record = new AdjustBill();
		record.setSchool_id(schoolId);
		record.setType2(type2);
		return adjustBillMapper.select(record);
	}

	@Override
	public List<AdjustBill> queryAdjustBillBySchoolIdAndTeacherIdAndAdjustDateAndOldSubjectAndOldClassAndOldNode(
			AdjustBill adjustBill) {
		AdjustBill record = new AdjustBill();
		record.setSchool_id(adjustBill.getSchool_id());
		record.setTeacher_id(adjustBill.getTeacher_id());
		record.setAdjust_date(adjustBill.getAdjust_date());
		record.setOld_class(adjustBill.getOld_class());
		record.setOld_subject(adjustBill.getOld_subject());
		record.setOld_class(adjustBill.getOld_class());
		record.setOld_node(adjustBill.getOld_node());
		return adjustBillMapper.select(record);
	}

	@Override
	public List<AdjustBill> queryAdjustBySchoolIdAndStatusAndType2(String schoolId, int i, int j) {
		AdjustBill record = new AdjustBill();
		record.setSchool_id(schoolId);
		record.setStatus(i);
		record.setType2(j);
		return adjustBillMapper.select(record);
	}

	@Override
	public List<AdjustBill> queryAllAdjustBillBySchoolIdAndPersionIsNull(String schoolId, Integer type2) {
		Example example = new Example(AdjustBill.class);
		example.orderBy("createDate").asc(); // 升序
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("school_id", schoolId);
		criteria.andEqualTo("type2", type2);
		criteria.andIsNull("petitioner_id");
		return adjustBillMapper.selectByExample(example);
	}

	@Override
	public Bill queryBillById(long billId) {
		AdjustBill adjustBill = adjustBillMapper.selectByPrimaryKey(billId);
		Bill bill = new Bill();
		bill.setType(adjustBill.getType2()+1);
		bill.setTeacherName(adjustBill.getTeacher_name());
		bill.setAdjustDate(adjustBill.getAdjust_date());
		bill.setWeek(adjustBill.getWeek());
		bill.setOldNode(adjustBill.getOld_node());
		bill.setPetitionerName(adjustBill.getPetitioner_name());
		bill.setPetitionerDate(adjustBill.getPetitioner_date());
		bill.setPetitionerWeek(adjustBill.getPetitioner_week());
		bill.setCourseNum(adjustBill.getCourse_num());
		return bill;
	}

}
